package com.example.smartcommunity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.smartcommunity.com.example.utils.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class RetrieveActivity extends AppCompatActivity {
    private Button bt_verification,bt_registered;
    private EditText pwd_name,pwd_phone,pwd_verification,pwd_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
        bt_verification = findViewById(R.id.bt_verification);
        bt_registered = findViewById(R.id.bt_registered);

        pwd_name = findViewById(R.id.pwd_name);
        pwd_phone = findViewById(R.id.pwd_phone);
        pwd_verification = findViewById(R.id.pwd_verification);
        pwd_password = findViewById(R.id.pwd_password);

        bt_registered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String pwd_nameStr = pwd_name.getText().toString();
               String pwd_phoneStr = pwd_phone.getText().toString();
               String pwd_verificationStr = pwd_verification.getText().toString();
               String pwd_passwordStr = pwd_password.getText().toString();

                if (TextUtils.isEmpty(pwd_nameStr)) {
                    Toast.makeText(RetrieveActivity.this, "输入用户名", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(pwd_phoneStr)) {
                    Toast.makeText(RetrieveActivity.this, "输入密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(pwd_verificationStr)) {
                    Toast.makeText(RetrieveActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(pwd_passwordStr)){
                    Toast.makeText(RetrieveActivity.this, "输入验证码", Toast.LENGTH_SHORT).show();
                } else {
                    Map<String, Object> map = new HashMap<>();
                    OkHttpUtils.getDataFromIntent("updpassword?username=" + pwd_nameStr + "&password=" + pwd_passwordStr, map, new OkHttpUtils.CallBack() {
                        @Override
                        public void successful(String string) {
                            try {
                                JSONObject jsonObject = new JSONObject(string);
                                String msg = jsonObject.getString("msg");
                                Log.e("TAG",msg);
                                if (msg.equals("用户名或密码错误")) {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                } else {
                                    startActivity(new Intent(RetrieveActivity.this, LoginActivity.class));
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            RetrieveActivity.this.finish();
                        }

                        @Override
                        public void onErrer(Exception e) {
                            Log.e("请求错误", e.getMessage());
                        }
                    });
                }
            }
        });
    }
}
